# Maintainer: David Runge <dvzrv@archlinux.org>

pkgname=openpgp-card-tools
pkgver=0.9.5
pkgrel=1
pkgdesc="CLI tool to inspect, manage and use OpenPGP cards"
arch=(loong64 x86_64)
url="https://codeberg.org/openpgp-card/openpgp-card-tools"
license=('Apache-2.0 OR MIT')
depends=(
  ccid
  gcc-libs
  glibc
  gmp
)
makedepends=(
  bzip2
  cargo
  clang
  nettle
  pcsclite
)
source=($pkgname-$pkgver.tar.gz::$url/archive/v$pkgver.tar.gz)
sha512sums=('25a8cdd496e4a3292096db94bd4ecb35f693a73ad2b6f8407084537350fdec5fc9ce8a88b12b8d8c3f82aff3c2e63dfc9c3130ec9c9bcf1ee5bf6100a7e8e998')
b2sums=('1fc6e02038d7cd9ba561c00f0c9b11cd27f282931b1fa70cb2436917224a1fd63c0e130e0d1c9c68f9db082ac596380741b7dac164e4ab7d58a54ad70157e3d9')

prepare() {
  cd $pkgname
  cargo fetch --locked --target "$CARCH-unknown-linux-gnu"
}

build() {
  cd $pkgname
  export RUSTUP_TOOLCHAIN=stable
  export CARGO_TARGET_DIR=target
  cargo build --frozen --release
  OCT_MANPAGE_OUTPUT_DIR="$PWD/../man/" cargo run
  OCT_COMPLETION_OUTPUT_DIR="$PWD/../completion/" cargo run
}

check() {
  cd $pkgname
  export RUSTUP_TOOLCHAIN=stable
  cargo test --frozen --all-features
}

package() {
  depends+=(
    bzip2 libbz2.so
    nettle libhogweed.so libnettle.so
    pcsclite libpcsclite.so
  )

  cd $pkgname
  install -vDm 755 target/release/opgpcard -t "$pkgdir/usr/bin/"
  install -vDm 644 {scripting,README}.md -t "$pkgdir/usr/share/doc/$pkgname/"
  install -vDm 644 LICENSES/MIT.txt -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 ../man/*.1 -t "$pkgdir/usr/share/man/man1/"
  install -vDm 644 ../completion/opgpcard.bash "$pkgdir/usr/share/bash-completion/completions/opgpcard"
  install -vDm 644 ../completion/_opgpcard -t "$pkgdir/usr/share/zsh/site-functions/"
  install -vDm 644 ../completion/opgpcard.fish -t "$pkgdir/usr/share/fish/vendor_completions.d/"
}
